package com.bifrost.admin.controller;

import com.bifrost.annotation.CurrentAdmin;
import com.bifrost.entity.User;
import com.bifrost.model.ResultModel;
import com.bifrost.model.ReturnAuditModel;
import com.bifrost.model.vo.OrderReturnItemVO;
import com.bifrost.model.vo.PageVO;
import com.bifrost.service.AdminOrderReturnService;
import com.bifrost.service.AdminUserService;
import com.bifrost.util.QueryUtils;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Api("后台退单相关接口")
@RequestMapping("/admin/orderreturn")
@RestController
@Validated
public class AdminOrderReturnController {

    @Autowired
    private AdminOrderReturnService adminOrderReturnService;
    @Autowired
    private AdminUserService userService;

    @GetMapping("/page")
    @ApiOperation("分页查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "returnType", value = "查询条件(退单类型)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderReturnNumber" , value = "查询条件(退单号)" , dataType = "String" , paramType = "query"),
            @ApiImplicitParam(name = "originalOrderNumber" , value = "查询条件(原订单号)" , dataType = "String" , paramType = "query"),
            @ApiImplicitParam(name = "orderReturnStatus" , value = "查询条件(状态PURCHASED提交、CONFIRMED审核通过、SHIPPED已回寄商品、CANCELED取消、FINISHED已收货)",dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "merchantStoreIds" , value = "查询条件(门店ids)",dataType = "数组" ,paramType = "query"),
            @ApiImplicitParam(name = "skuExternalId" , value = "查询条件(商品sap编码)",dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "productName" , value = "查询条件(商品名称)",dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "startTime" , value = "查询条件(订单创建时间的左区间)",dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "endTime" , value = "查询条件(订单创建时间的右区间)",dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "pageCode" , value = "当前页(默认为第一页)" , dataType = "int" , paramType = "query"),
            @ApiImplicitParam(name = "pageSize" , value = "单页显示条数(默认为10个)" ,  dataType = "int" , paramType = "query")
    })
    public ResultModel orderReturnPages(String returnType, @CurrentAdmin Long currentUser, String orderReturnNumber, String originalOrderNumber, String orderReturnStatus, Long[] merchantStoreIds, String skuExternalId, String productName, String startTime, String endTime, @RequestParam(defaultValue = "1") Integer pageCode, @RequestParam(defaultValue = "10") Integer pageSize){
        try{
            List<Long> merchantStoreIdList = Lists.newArrayList();
            merchantStoreIdList = QueryUtils.getMerchantIds(userService.findById(currentUser) ,merchantStoreIds);
            PageVO pageVO = adminOrderReturnService.findByPage(returnType, orderReturnNumber,originalOrderNumber,orderReturnStatus,merchantStoreIdList,skuExternalId,productName,startTime,endTime,pageCode,pageSize);
            if(!CollectionUtils.isEmpty(pageVO.getRecordList())){
                List<OrderReturnItemVO> recordList = pageVO.getRecordList();
                // 按照退单id降序排列
                Collections.sort(recordList, new Comparator<OrderReturnItemVO>() {
                    @Override
                    public int compare(OrderReturnItemVO orderReturnItemVO1, OrderReturnItemVO orderReturnItemVO2) {
                        if (orderReturnItemVO1.getOrderReturnVO().getId() < orderReturnItemVO1.getOrderReturnVO().getId()) {
                            return 1;
                        }
                        if (orderReturnItemVO1.getOrderReturnVO().getId() == orderReturnItemVO1.getOrderReturnVO().getId()) {
                            return 0;
                        }
                        return -1;
                    }
                });
                return ResultModel.ok().put("customerTagsList",pageVO);
            }
            return ResultModel.error("没有数据");
        }catch (Exception e){
            e.printStackTrace();
            return ResultModel.error("出现一些未知错误");
        }
    }

    @PostMapping("/audit")
    public ResultModel audit(@CurrentAdmin Long currentUser,@RequestBody @Valid ReturnAuditModel auditModel) {
        adminOrderReturnService.audit(userService.findById(currentUser),auditModel);
        return ResultModel.ok("完成审核，已进入审核列表中..");
    }

    @PostMapping("/refund")
    public ResultModel refund(@CurrentAdmin Long currentUser,Long orderReturnId) {
        adminOrderReturnService.refund(userService.findById(currentUser),orderReturnId);
        return ResultModel.ok("退款申请已发送。。。。");
    }
}
